﻿<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="Cybozu.SharePoint.ListItemCopySB.ListItemButton" Location="CommandUI.Ribbon.ListView" RegistrationType="List" RegistrationId="100">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.ListItem.Actions.Controls._children">
          <Button
            Id="Ribbon.ListItem.Actions.SPListItemCopy"
            Command="ItemCopy"
            Image16by16="/_layouts/$Resources:core,Language;/images/formatmap16x16.png"
            Image16by16Top="-224"
            Image16by16Left="-48"
            Image32by32="/_layouts/$Resources:core,Language;/images/formatmap32x32.png"
            Image32by32Top="-320"
            Image32by32Left="-384"
            LabelText="Copy Item"
            Sequence="200"
            TemplateAlias="o1"
            ToolTipTitle="Copy Item"
            ToolTipDescription="Copy the selected item." />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler Command="ItemCopy"
                          CommandAction="javascript:
                          function copyListItems() {
                              var res;
                              if (SPListItemCopy.Resources[_spPageContextInfo.currentLanguage]) {
                                  res = SPListItemCopy.Resources[_spPageContextInfo.currentLanguage];
                              } else {
                                  res = SPListItemCopy.Resources[SPListItemCopy.DefaultLanguage];
                              }
                              if (!confirm('SP List Item Copy\n\n' + res.CopyItemConfirm)) return;
                              var items = SP.ListOperation.Selection.getSelectedItems();
                              if (items.length == 0) return;
                              var id;
                              for (var i in items) {
                                  id = items[i].id;
                                  break;
                              }
                              var listId = SP.ListOperation.Selection.getSelectedList();
                              var context = SP.ClientContext.get_current();
                              var list = context.get_web().get_lists().getById(listId);
                              var item = list.getItemById(id);
                              var contentType = item.get_contentType();
                              var fields = contentType.get_fields();
                              context.load(item);
                              context.load(fields);
                              context.executeQueryAsync(function () {
                                  var itemCreateInfo = new SP.ListItemCreationInformation();
                                  var newItem = list.addItem(itemCreateInfo);
                                  var fieldEnumerator = fields.getEnumerator();
                                  while (fieldEnumerator.moveNext()) {
                                      var field = fieldEnumerator.get_current();
                                      if (field.get_readOnlyField()) continue;
                                      var name = field.get_internalName();
                                      if (name == 'ContentType') name = 'ContentTypeId';
                                      var value = item.get_item(name);
                                      if (name == 'Title') {
                                          if (value == null) value = '';
                                          value += ' - Copy';
                                      }
                                      newItem.set_item(name, value);
                                  }
                                  newItem.update();
                                  context.executeQueryAsync(function () {
                                      var url = location.href;
                                      if (0 > url.indexOf('?')) {
                                          url += '?InitialTabId=Ribbon%2EListItem\u0026VisibilityContext=WSSTabPersistence';
                                      } else if (0 > url.indexOf('InitialTabId')) {
                                          url += '\u0026InitialTabId=Ribbon%2EListItem\u0026VisibilityContext=WSSTabPersistence';
                                      }
                                      SP.Utilities.HttpUtility.navigateTo(url);
                                  }, function () {
                                      alert(res.CopyItemFail);
                                  });
                              });
                          }
                          copyListItems();"

                          EnabledScript="javascript:(SP.ListOperation.Selection.getSelectedItems().length == 1);" />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
  <CustomAction
    Id="Cybozu.SharePoint.ListItemCopySB.ItemCopyScript"
    Location="ScriptLink"
    ScriptSrc="~Site/Lists/SPListItemCopyFiles/ItemCopy.js"
    Sequence="100" />
</Elements>
